package com.his.modules.business.service;

import com.his.core.utils.StringUtils;
import com.his.modules.business.dao.MedicalRecordMapper;
import com.his.modules.business.entity.*;
import com.his.modules.business.enums.BlflEnum;
import com.his.modules.business.enums.JzlxEnum;
import com.his.modules.common.util.CommonUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MedicalRecordService {
    @Resource
    private MedicalRecordMapper medicalRecordMapper;
    /**
     * 获取门诊病历列表
     * @return
     */
    public List<MedicalRecord> medicalRecordList(MedicalRecord medicalRecord) {
        if(BlflEnum.MZ.getCode().equals(medicalRecord.getBlfl())) {
            return medicalRecordMapper.mzMedicalRecordList(medicalRecord);
        } else if(BlflEnum.ZY.getCode().equals(medicalRecord.getBlfl())) {
            return medicalRecordMapper.zyMedicalRecordList(medicalRecord);
        } else {
            return null;
        }
    }
    /**
     * 获取Mrr版病历列表
     * @return
     */
    public List<MedicalRecord> medicalRecordListMrr(MedicalRecord medicalRecord) {
        if(BlflEnum.MZ.getCode().equals(medicalRecord.getBlfl())) {
            return medicalRecordMapper.mzMedicalRecordListMrr(medicalRecord);
        } else if(BlflEnum.ZY.getCode().equals(medicalRecord.getBlfl())) {
            return medicalRecordMapper.zyMedicalRecordListMrr(medicalRecord);
        } else {
            return null;
        }
    }

    /**
     * 获取Mrr版病历(单病历)
     * @return
     */
    public MedicalRecord medicalRecordInfoMrr(MedicalRecord medicalRecord) {
        if(BlflEnum.MZ.getCode().equals(medicalRecord.getBlfl())) {
            return medicalRecordMapper.mzMedicalRecordInfoMrr(medicalRecord);
        } else if(BlflEnum.ZY.getCode().equals(medicalRecord.getBlfl())) {
            return medicalRecordMapper.zyMedicalRecordInfoMrr(medicalRecord);
        } else {
            return null;
        }
    }

    /**
     * 获取门诊病历列表
     * @return
     */
    public List<MedicalRecord> medicalRrdList(MedicalRecord medicalRecord) {
        if(BlflEnum.MZ.getCode().equals(medicalRecord.getBlfl())) {
            return medicalRecordMapper.mzMrdList(medicalRecord);
        } else if(BlflEnum.ZY.getCode().equals(medicalRecord.getBlfl())) {
            return medicalRecordMapper.zyMrdList(medicalRecord);
        } else {
            return null;
        }
    }
    /**
     * 将病历解析结果存库
     * @param medicalRecordAnalysis
     * @param medicalRecordAnalysisRecordList
     */
    @Transactional(readOnly = false)
    public void medicalRecordAnalysis(MedicalRecordAnalysis medicalRecordAnalysis, List<MedicalRecordAnalysisRecord> medicalRecordAnalysisRecordList) {
        //先删再增，防止重复解析操作生成重复数据
        medicalRecordMapper.deleteMedicalRecordAnalysis(medicalRecordAnalysis);
        String blfl = medicalRecordAnalysis.getBlfl();
        if(BlflEnum.MZ.getCode().equals(blfl)) {
            medicalRecordMapper.deleteMedicalRecordAnalysisRecordMz(medicalRecordAnalysis);
        } else if(BlflEnum.ZY.getCode().equals(blfl)) {
            medicalRecordMapper.deleteMedicalRecordAnalysisRecordZy(medicalRecordAnalysis);
        }
        //保存解析结果
        medicalRecordAnalysis.setJlxh(CommonUtils.getSequence("EMR_BL_ANALYSIS", 1));
        medicalRecordMapper.saveMedicalRecordAnalysis(medicalRecordAnalysis);
        if(!CollectionUtils.isEmpty(medicalRecordAnalysisRecordList)) {
            Long sequence, jlxh;
            int size = medicalRecordAnalysisRecordList.size();
            if(BlflEnum.MZ.getCode().equals(blfl)) {
                sequence = CommonUtils.getSequence("EMR_BL_ANALYSIS_RECORD_MZ", size);
                jlxh = sequence - size + 1;
                for(MedicalRecordAnalysisRecord medicalRecordAnalysisRecord : medicalRecordAnalysisRecordList) {
                    medicalRecordAnalysisRecord.setJlxh(jlxh);
                    if(StringUtils.getActLen(medicalRecordAnalysisRecord.getYsqz()) >= 4000) {
                        medicalRecordAnalysisRecord.setYsqz(medicalRecordAnalysisRecord.getYsqz().substring(0, 2000));
                    }
                    medicalRecordMapper.saveMedicalRecordAnalysisRecordMz(medicalRecordAnalysisRecord);
                    jlxh ++;
                }
            } else if(BlflEnum.ZY.getCode().equals(blfl)) {
                sequence = CommonUtils.getSequence("EMR_BL_ANALYSIS_RECORD_ZY", size);
                jlxh = sequence - size + 1;
                for(MedicalRecordAnalysisRecord medicalRecordAnalysisRecord : medicalRecordAnalysisRecordList) {
                    medicalRecordAnalysisRecord.setJlxh(jlxh);
                    if(StringUtils.getActLen(medicalRecordAnalysisRecord.getYsqz()) >= 4000) {
                        medicalRecordAnalysisRecord.setYsqz(medicalRecordAnalysisRecord.getYsqz().substring(0, 2000));
                    }
                    medicalRecordMapper.saveMedicalRecordAnalysisRecordZy(medicalRecordAnalysisRecord);
                    jlxh ++;
                }
            }
        }
    }

    /**
     * Mrr版解析服务
     * @param medicalRecordAnalysis
     * @param medicalRecordAnalysisRecordList
     */
    @Transactional(readOnly = false)
    public void medicalRecordAnalysisMrr(MedicalRecordAnalysis medicalRecordAnalysis, List<MedicalRecordAnalysisRecord> medicalRecordAnalysisRecordList, List<MedicalRecordAnalysisRecord> paraList) {
        String bldlTable = "MRR_BLDL" + medicalRecordAnalysis.getYdlbbm();
        String blysTable = "MRR_BLYS" + medicalRecordAnalysis.getYdlbbm();

        String blfl = medicalRecordAnalysis.getBlfl();
        MrrJxjl mrrJxjl = new MrrJxjl();
        mrrJxjl.setBlbh(medicalRecordAnalysis.getBlbh());

        if(BlflEnum.MZ.getCode().equals(blfl)) {
            mrrJxjl.setJzlx(JzlxEnum.MZ.getCode());
        } else if(BlflEnum.ZY.getCode().equals(blfl)) {
            mrrJxjl.setJzlx(JzlxEnum.ZY.getCode());
        }

        //先删再增，防止重复解析操作生成重复数据
        medicalRecordMapper.deleteMedicalRecordAnalysisMrrJxjl(mrrJxjl);
        //保存解析结果
        mrrJxjl.setJlxh(CommonUtils.getSequence("EMR_MRR_JXJL", 1));
        mrrJxjl.setJxzt(1);
        medicalRecordMapper.saveMedicalRecordAnalysisMrrJxjl(mrrJxjl);

        //删除相应的段落及元素解析内容
        medicalRecordAnalysis.setDlTableName(bldlTable);
        medicalRecordAnalysis.setYsTableName(blysTable);
        //根据病历编号删除段落解析内容
        medicalRecordMapper.deleteMedicalRecordAnalysisRecordBldl(medicalRecordAnalysis);
        //根据病历编号删除元素解析内容
        medicalRecordMapper.deleteMedicalRecordAnalysisRecordBlys(medicalRecordAnalysis);

//        if(StringUtils.isNotBlank(medicalRecordAnalysisRecord.getDlid())) {
//            jlxh = CommonUtils.getSequence("EMR_MRR_BLDL", 1);
//            medicalRecordAnalysisRecord.setDlTableName(bldlTable);
//            medicalRecordAnalysisRecord.setJlxh(jlxh);
//            medicalRecordMapper.saveMedicalRecordAnalysisRecordMrrBldl(medicalRecordAnalysisRecord);
//        } else


        if(!CollectionUtils.isEmpty(medicalRecordAnalysisRecordList)) {
            Long jlxh;
            MedicalRecordAnalysisRecord first = null;

            for(MedicalRecordAnalysisRecord medicalRecordAnalysisRecord : medicalRecordAnalysisRecordList) {
                if(StringUtils.isBlank(medicalRecordAnalysisRecord.getYsqz())
                        || StringUtils.isBlank(medicalRecordAnalysisRecord.getYsmc())
                        || StringUtils.isBlank(medicalRecordAnalysisRecord.getDlmc())) {
                    continue;
                }
                if(StringUtils.isBlank(medicalRecordAnalysisRecord.getYsqz2())) {
                    medicalRecordAnalysisRecord.setYsqz2("0.0000");
                }
                if(StringUtils.isBlank(medicalRecordAnalysisRecord.getDlmc())) {
                    medicalRecordAnalysisRecord.setDlmc("上级输入域未设置为段落请设置（只支持两级输入域）");
                }
                if(BlflEnum.MZ.getCode().equals(blfl)) {
                    medicalRecordAnalysisRecord.setJzlx(3);
                    medicalRecordAnalysisRecord.setBlly(2);
                } else if(BlflEnum.ZY.getCode().equals(blfl)) {
                    medicalRecordAnalysisRecord.setJzlx(1);
                    medicalRecordAnalysisRecord.setBlly(1);
                }
                if(StringUtils.getActLen(medicalRecordAnalysisRecord.getYsqz()) >= 4000) {
                    medicalRecordAnalysisRecord.setYsqz(medicalRecordAnalysisRecord.getYsqz().substring(0, 2000));
                }

                jlxh = CommonUtils.getSequence("EMR_MRR_BLYS", 1);
                medicalRecordAnalysisRecord.setYsTableName(blysTable);
                medicalRecordAnalysisRecord.setJlxh(jlxh);
                medicalRecordMapper.saveMedicalRecordAnalysisRecordMrrBlys(medicalRecordAnalysisRecord);

                if(null == first) {
                    first = new MedicalRecordAnalysisRecord();
                    BeanUtils.copyProperties(medicalRecordAnalysisRecord, first);
                }

                MrrMbys mrrMbys = new MrrMbys();
                mrrMbys.setBllb(Long.parseLong(medicalRecordAnalysisRecord.getBllb()));
                mrrMbys.setMblb(Long.parseLong(medicalRecordAnalysisRecord.getMblb()));
                mrrMbys.setDlmc(medicalRecordAnalysisRecord.getDlmc());
                mrrMbys.setYsmc(medicalRecordAnalysisRecord.getYsmc());
                mrrMbys.setFhys(medicalRecordAnalysisRecord.getYsmc());
                Integer count = medicalRecordMapper.mrrMbysCount(mrrMbys);
                if(count == 0) {
                    Long mbys = CommonUtils.getSequence("EMR_MRR_MBYS", 1);
                    mrrMbys.setJgbh(mbys);
                    medicalRecordMapper.saveMrrMbys(mrrMbys);
                }
            }

            if(!CollectionUtils.isEmpty(paraList)) {
                Long dlxh;
                for(MedicalRecordAnalysisRecord para : paraList) {
                    if(StringUtils.isBlank(para.getDlqz())) {
                        continue;
                    }
                    dlxh = CommonUtils.getSequence("EMR_MRR_BLDL", 1);
                    para.setDlTableName(bldlTable);
                    para.setJlxh(dlxh);
                    para.setBlbh(first.getBlbh());
                    para.setBllb(first.getBllb());
                    para.setJzxh(first.getJzxh());
                    para.setJzlx(first.getJzlx());
                    para.setBlly(first.getBlly());
                    para.setBllx(first.getBllx());
                    para.setMblb(first.getMblb());
                    para.setMbbh(first.getMbbh());
                    if(StringUtils.getActLen(para.getDlqz()) >= 4000) {
                        para.setDlqz(para.getDlqz().substring(0, 2000));
                    }
                    medicalRecordMapper.saveMedicalRecordAnalysisRecordMrrBldl(para);
                }
            }
        }
    }
}